package cn.newtest.bi.collection.groovy;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
import groovy.util.GroovyScriptEngine;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;

 
public class GChecker {
	private final org.apache.log4j.Logger log = org.apache.log4j.Logger
			.getLogger(GChecker.class);

	public String checkAll(Hashtable reportInfo) throws Exception {
		BufferedReader br = null;
		InputStream inputStream = null;
		String fileName = "formula.gs";
		String srcPath = "";
		try {
			srcPath = Thread.currentThread().getContextClassLoader()
					.getResource("").getPath();


			// 请注意不要"/risereportconfig/" + actionConfigFileName最前面的 /

			if (Thread.currentThread().getContextClassLoader()
					.getResourceAsStream("groovy/" + fileName).available() > 0) {
				try {
					inputStream = Thread.currentThread()
							.getContextClassLoader().getResourceAsStream(
									"groovy/" + fileName);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				br = new BufferedReader(new InputStreamReader(inputStream));

			} else {
				log.debug("无法找到配置文件：" + fileName);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (br != null)
				br.close();
			if (inputStream != null)
				inputStream.close();
		}

		try {
			String[] roots = new String[] { srcPath + "groovy" };
			GroovyScriptEngine gse = new GroovyScriptEngine(roots);
			Binding binding = new Binding();
			binding.setVariable("reportInfo", reportInfo);
			gse.run(fileName, binding);
			log.debug(binding.getVariable("output"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (br != null)
				br.close();
			if (inputStream != null)
				inputStream.close();
		}

		return "";
	}
	
	public ArrayList checkAll(Hashtable reportInfo,String newScript) throws Exception {
		ArrayList gresult = new ArrayList();
		try {
			GroovyShell shell = new GroovyShell();
			Script scrpt = shell.parse(newScript);
			Binding binding = new Binding();
			binding.setVariable("reportInfo", reportInfo);
			scrpt.setBinding(binding);
			scrpt.run();
			Object output = binding.getVariable("output");
			if(output!=null)gresult = (ArrayList)output;
		} catch (Exception e) { 
			e.printStackTrace();
			throw new Exception("系统公式错误:"+e.getMessage());
		} 
		return gresult;
	}
	
	public String checkSO(HashMap soInfo,String newScript) throws Exception {
		String gresult = "";
		try {
			GroovyShell shell = new GroovyShell();
			Script scrpt = shell.parse(newScript);
			Binding binding = new Binding();
			binding.setVariable("soInfo", soInfo);
			scrpt.setBinding(binding);
			scrpt.run();
			Object output = binding.getVariable("output");
			if(output!=null)gresult = (String)output;
		} catch (Exception e) { 
			e.printStackTrace();
			throw new Exception("系统公式错误:"+e.getMessage());
		} 
		return gresult;
	}

	public static void main(String args[]) throws Throwable {
		GChecker checker = new GChecker();
		checker.checkAll(null);
	}
}
